package com.cn.xiaonuo.modular.service.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.util.DateUtils;
import com.cn.xiaonuo.core.util.StringUtils;
import com.cn.xiaonuo.modular.dao.GroupApplyDao;
import com.cn.xiaonuo.modular.dao.GroupInfoDao;
import com.cn.xiaonuo.modular.dao.GroupMsgDao;
import com.cn.xiaonuo.modular.dao.GroupUserDao;
import com.cn.xiaonuo.modular.model.app.GroupApply;
import com.cn.xiaonuo.modular.model.app.GroupInfo;
import com.cn.xiaonuo.modular.model.app.GroupMsg;
import com.cn.xiaonuo.modular.model.app.GroupUser;
import com.cn.xiaonuo.modular.model.app.UserClassmate;
import com.cn.xiaonuo.modular.model.app.UserClassmateApply;
import com.cn.xiaonuo.modular.model.app.UserClassmateMsg;
import com.cn.xiaonuo.modular.utlis.IDGenerator;

@Service
public class GroupService {
	
	@Autowired
	private GroupApplyDao groupApplyDao;
	@Autowired
	private IDGenerator idGenerator;
	@Autowired
	private GroupMsgDao groupMsgDao;
	@Autowired
	private GroupInfoDao groupInfoDao;
	@Autowired
	private GroupUserDao groupUserDao;
	
	/** 创建读书会 */
	public void createGroupInfo(GroupInfo info) {
		groupInfoDao.insert(info);
	}
	
	/** 修改读书会 */
	public void updateGroupInfo(GroupInfo info) {
		groupInfoDao.updateTemplateById(info);
	}
	
	/** 删除读书会 */
	public void deleteGroupInfo(Long userId,Long groupId) {
		GroupInfo single = groupInfoDao.single(groupId);
		if(null == single) {
			throw new ServiceException(9999,"当前操作数据异常，请刷新后重试！");
		}
		if(userId != single.getGroupUserId()) {//不是群主
			throw new ServiceException(9999,"当前读书会为不可操作对象！");
		}
		groupInfoDao.deleteById(groupId);//删除群信息
		List<GroupUser> select = groupUserDao.createLambdaQuery()
		.andEq(GroupUser :: getGroupId, groupId).select();
		if(null == select || select.isEmpty()) {
			return;
		}
		List<GroupMsg> list = new ArrayList<>();
		for (GroupUser groupUser : select) {
			GroupMsg msg = new GroupMsg();
			msg.setGroupId(groupId);
			msg.setApplyUserId(groupUser.getUserId());
			msg.setCreateTime(DateUtils.getDate());
			msg.setId(idGenerator.generateCode("GM"));
			msg.setMsgType("4");
			msg.setStatus("0");
			list.add(msg);
		}
		groupMsgDao.insertBatch(list);//批量通知
		groupUserDao.createLambdaQuery()
			.andEq(GroupUser :: getGroupId, groupId).delete();//删除群成员
	}
	
	
	/** 消息通知 */
	public void addGroupApply(Long groupId,Long userId) {
		GroupInfo single = groupInfoDao.single(groupId);
		if(null == single) {
			throw new ServiceException(9999,"申请的读书会不存在或已解散！");
		}
		GroupApply apply = new GroupApply();
		apply.setGroupApplyId(idGenerator.generateCode("GA"));
		apply.setGroupId(groupId);
		apply.setApplyUserId(userId);
		apply.setCreateTime(DateUtils.getDate());
		apply.setStatus("0");//待处理状态
		apply.setUserId(single.getGroupUserId());
		groupApplyDao.insert(apply);
		GroupMsg msg = new GroupMsg();
		msg.setGroupId(groupId);
		msg.setApplyUserId(userId);
		msg.setUserId(single.getGroupUserId());
		msg.setAttach1(apply.getGroupApplyId());
		msg.setCreateTime(DateUtils.getDate());
		msg.setId(idGenerator.generateCode("GM"));
		msg.setMsgType("1");
		msg.setStatus("0");
		groupMsgDao.insert(msg);
	}
	
	/**
	 * 查询读书会消息通知
	 * @param userId
	 * @return
	 */
	public List<GroupMsg> findGroupMsgList(Long userId,int pageNo,int pageSize){
		return groupMsgDao.createLambdaQuery().andEq(GroupMsg :: getUserId, userId)
			.andNotEq(GroupMsg :: getStatus, "-1")
			.desc(GroupMsg :: getCreateTime).limit(pageNo, pageSize).select();
	}
	
	/**
	 * 操作数据已读或删除
	 * @param msgId
	 * @param opType
	 */
	public void opGroupMsg(Long msgId,String opType) {
		GroupMsg msg = groupMsgDao.single(msgId);
		if(null == msg) {
			throw new ServiceException(9999,"当前操作数据异常，请刷新后重试！");
		}
		if("1".equals(opType) && "1".equals(msg.getMsgType())) {//申请消息，只能操作，不能阅读
			return;
		}
		msg.setStatus(opType);
		groupMsgDao.updateTemplateById(msg);
	}
	
	/** 查询已加入群 */
	public List<GroupInfo> findGroupList(Long userId){
		return groupInfoDao.findGroupList(userId);
	}

	/** 处理申请 */
	public void opGroupApply(Long msgId, String groupApplyId, String opType) {
		GroupMsg msg = groupMsgDao.single(msgId);
		GroupApply apply = groupApplyDao.single(groupApplyId);
		if(null == apply || null == msg) {
			throw new ServiceException(9999,"对不起，当前网络异常，请稍后再试！");
		}
		GroupMsg msg1 = new GroupMsg();
		msg1.setCreateTime(new Date());
		msg1.setUserId(apply.getApplyUserId());
		msg1.setStatus("0");
		msg1.setMsgType(opType);
		msg1.setApplyUserId(apply.getUserId());
		groupMsgDao.insert(msg1);
		if("3".equals(opType)) {//同意申请
			GroupUser uc = new GroupUser();
			uc.setCreateTime(new Date());
			uc.setStatus("1");
			uc.setUserId(apply.getUserId());
			groupUserDao.insert(uc);
		}
		apply.setStatus(opType);//操作类型
		groupApplyDao.updateTemplateById(apply);
		msg.setStatus("1");//已处理
		groupMsgDao.updateTemplateById(msg);
	}
	
	public List<GroupInfo> searchGroupList(GroupInfo info,Integer pageNo,Integer pageSize){
		LambdaQuery<GroupInfo> createLambdaQuery = groupInfoDao.createLambdaQuery();
		if(null != info) {
			if(null != info.getApplyFlag()) {
				createLambdaQuery.andEq(GroupInfo :: getApplyFlag, info.getApplyFlag());
			}
			if(StringUtils.isNotEmpty(info.getGroupName())) {
				createLambdaQuery.andLike(GroupInfo :: getGroupName, info.getGroupName());
			}
		}
		createLambdaQuery.desc(GroupInfo :: getCreateTime).limit(pageNo, pageSize);
		return createLambdaQuery.select();
	}

}
